package com.changgou.goods.service.impl;


import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询所有品牌
     *
     * @return
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 通过id查询品牌
     *
     * @param id
     * @return
     */

    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加品牌
     *
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 更新品牌
     *
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 删除品牌
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 品牌列表条件查询
     *
     * @param brand
     * @return
     */
    @Override
    public List<Brand> findByExample(Brand brand) {
        Example example = createExample(brand);
        List<Brand> brands = brandMapper.selectByExample(example);
        return brands;
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Brand> brandList = brandMapper.selectAll();
        return new PageInfo(brandList);
    }

    /**
     * 分页条件查询
     *
     * @param brand
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo search(Brand brand, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        Example example = createExample(brand);

        List<Brand> brands = brandMapper.selectByExample(example);
        return new PageInfo(brands);
    }


    /**
     * 通过分类查询品牌列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> findBrandListByCategoryId(Integer categoryId) {

        List<Brand> brandList = brandMapper.findBrandListByCategoryId(categoryId);
        return brandList;
    }

    /**
     * 增加条件
     *
     * @param brand
     * @return
     */
    private Example createExample(Brand brand) {
        Example example = new Example(Brand.class);
        if (brand != null) {
            // 封装查询条件
            Example.Criteria criteria = example.createCriteria();
            // 名称模糊查询
            if (StringUtils.isNotEmpty(brand.getName())) {
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
            // 根据首字母查询
            if (StringUtils.isNotEmpty(brand.getLetter())) {
                criteria.andEqualTo("letter", brand.getLetter());
            }
            // 根据排序信息查询
            if (brand.getSeq() != null && StringUtils.isNotEmpty(brand.getSeq() + "")) {
                criteria.andEqualTo("seq", brand.getSeq());
            }
        }
        return example;
    }
}
